<?php

namespace app\admin\model;

use think\Model;
use fast\Http;

class Caijishuju extends Model
{

    

    

    // 表名
    protected $name = 'caijishuju';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];
    public function caiji($no){
		set_time_limit(0);
        // $no = 'JT4283054732340';
        $url = 'https://wdgw.jtexpress.com.cn/sqsthird/thirdService/waybill/commonWaybillListByWaybillNos';
        $token = '0219efded696462d9c3b9fb7f24c0a4b';
        $res = Http::get($url,['waybillNos'=>$no]);
        $json = json_decode($res,1);
        \think\log::record("sraech".$res);
        // dump($json);
		if($json['code'] =='1'){
			foreach($json['data'] as $val){
				// dump($val);exit;
				$d = self::where(['expressno'=>$val['waybillNo']])->find();
				if(!$d){
					$return_phone = self:: getphone($val['waybillNo'],$val['orderSourceName'],$val['orderSourceCode']);
					if(!isset($return_phone['receiverMobilePhone']) && !isset($return_phone['receiverTelphone'])){
				// 		break;
						return ['code'=>'0','msg'=>'登录账号token已经到达限制,请切换token!'];
							break;
					}
					$insert=[
						'expressno'=>$val['waybillNo'],
						'name'=>$val['receiverName'],
						'phone'=>isset($return_phone['receiverMobilePhone']) ? $return_phone['receiverMobilePhone'] : $return_phone['receiverTelphone'],
						'address'=>$val['receiverProvinceName']." ".$val['receiverCityName']." ".$val['receiverAreaName']." ".$val['receiverDetailedAddress'],
						'orderSourceName'=>$val['orderSourceName'],
						'orderSourceCode'=>$val['orderSourceCode'],
						'createtime'=>time(),
						'status'=>'1',
						'data'=>json_encode($val,1)
					];
					self::insert($insert);
					$send = rand(100,500)/100;
					sleep($send);
				}
				// 	dump($d);
				if(isset($d)){
					$return_phone = self:: getphone($d['expressno'],$d['orderSourceName'],$d['orderSourceCode']);
				// 		dump($return_phone);
					if(!isset($return_phone['receiverMobilePhone']) && !isset($return_phone['receiverTelphone'])){
						break;
						return ['code'=>'0','msg'=>'登录账号token已经到达限制,请切换token'];
					}
				// 	dump($return_phone);
					$insert=[
						'name'=>$val['receiverName'],
						'phone'=>isset($return_phone['receiverMobilePhone']) ? $return_phone['receiverMobilePhone'] : $return_phone['receiverTelphone'],
						'address'=>$val['receiverProvinceName']." ".$val['receiverCityName']." ".$val['receiverAreaName']." ".$val['receiverDetailedAddress'],
						'orderSourceName'=>$val['orderSourceName'],
						'orderSourceCode'=>$val['orderSourceCode'],
						'data'=>json_encode($val,1),
						'status'=>'1',
					];
				    // 	dump($insert);
					self::where(['id'=>$d['id']])->update($insert);
				}
			}
			return ['code'=>'1','msg'=>'获取成功'];
		}else{
			return ['code'=>'0','msg'=>'登录账号token过期,请重新登录后获取'];
		}
        // dump($json);
		// self::getphone($no,$orderSourceName,$orderSourceCode);
    }
    public function getphone($no,$orderSourceName,$orderSourceCode){
        $url = 'https://wdgw.jtexpress.com.cn/sqsthird/integration/getWaybillsByReverse';
        $data=[
            'waybillId'=>$no,
            'queryReason'=>'YTH',
            'orderSourceName'=>$orderSourceName,
            'orderSourceCode'=>$orderSourceCode
        ];
        $res = Http::get($url,$data);
		 \think\log::record("getphone".$res);
        $json = json_decode($res,1);
        // dump($json);
		if($json['code'] =='1'){
			 return $json['data'];
		}else{
			echo $json['msg'];exit;
		}
        return $json;
    }

    







}